#include "StdAfx.h"
#include "HotKey.h"

//------------------------------------------------------------------------

HotKey::HotKey()
:m_id(0)
,m_vk_code(0)
,m_modifiers(MOD_NONE)
,m_is_registered(false)
  {
  }

//------------------------------------------------------------------------

HotKey::HotKey(int i_id, KeyCode i_vk_code, 
               HotKeyOperation::IOperation * ip_operation /*= 0*/,
               const std::wstring& i_name /* = "" */,
               Modifier i_modifiers /* = MOD_NONE */)
:m_id(i_id)
,m_vk_code(i_vk_code)
,m_modifiers(i_modifiers)
,m_name(i_name)
,m_is_registered(false)
,mp_operation(ip_operation)
  {
  Register();
  }

//------------------------------------------------------------------------

HotKey::~HotKey(void)
  {
  if (m_is_registered)
    UnregisterHotKey(0, m_id);
  }

//------------------------------------------------------------------------

bool HotKey::Register()
  {
  if (!m_is_registered)
    {
    m_is_registered = (RegisterHotKey(0, m_id, m_modifiers, m_vk_code) == TRUE) ?
      true : false;
    }
  return m_is_registered;
  }

//------------------------------------------------------------------------

bool HotKey::Unregister()
  {
  if (m_is_registered)
    {
    m_is_registered = (UnregisterHotKey(0, m_id) == TRUE) ?
      false : true;
    }
  return !m_is_registered;
  }

//------------------------------------------------------------------------

void HotKey::Handle()
  {
  if (mp_operation.get() != 0)
    mp_operation->Operate();
  }

//------------------------------------------------------------------------

//------------------------------------------------------------------------
// Setters section
//------------------------------------------------------------------------

void HotKey::SetID(int i_id)
  {
  m_id = i_id;
  }

//------------------------------------------------------------------------

void HotKey::SetName(std::wstring & i_name)
  {
  m_name = i_name;
  }

//------------------------------------------------------------------------

void HotKey::SetVKCode(KeyCode i_vk_code)
  {
  m_vk_code = i_vk_code;
  }

//------------------------------------------------------------------------

void HotKey::SetModifiers(Modifier i_modifiers /* = MOD_NONE */)
  {
  m_modifiers = i_modifiers;
  }

//------------------------------------------------------------------------

void HotKey::AddModifiers(Modifier i_modifiers)
  {
  m_modifiers |= i_modifiers;
  }

//------------------------------------------------------------------------

void HotKey::RemoveModifiers(Modifier i_modifiers)
  {
  i_modifiers ^= i_modifiers;  
  }

//------------------------------------------------------------------------

//------------------------------------------------------------------------
// Getters section
//------------------------------------------------------------------------

int HotKey::GetID() const
  {
  return m_id;
  }

//------------------------------------------------------------------------

std::wstring HotKey::GetName() const
  {
  return m_name;
  }

//------------------------------------------------------------------------

HotKey::KeyCode HotKey::GetVKCode() const
  {
  return m_vk_code;
  }

//------------------------------------------------------------------------

HotKey::Modifier HotKey::GetModifiers() const
  {
  return m_modifiers;
  }

//------------------------------------------------------------------------

bool HotKey::GetIsRegistered() const
  {
  return m_is_registered;
  }

//------------------------------------------------------------------------

  bool operator==(const HotKey & i_hot_key_left, const HotKey & i_hot_key_right)
    {
    return (i_hot_key_left.GetVKCode() == i_hot_key_right.GetVKCode() &&
      i_hot_key_left.GetModifiers() == i_hot_key_right.GetModifiers());
    }

//------------------------------------------------------------------------
